//
//  OMCategoryFilterSelectionView.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/18.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMCategoryFilterSelectionView.h"
#import "BaseCollectionViewCell.h"
@interface OMCategoryFilterSelectionCollectCell :BaseCollectionViewCell
@property (nonatomic, strong) UILabel *elementLabel;
@property (nonatomic, assign) BOOL om_selected;
@end

@implementation OMCategoryFilterSelectionCollectCell
- (void)configure
{
    [super configure];
    _elementLabel = [UILabel new];
    _elementLabel.font = SM_CFont(30);
    _elementLabel.textAlignment = NSTextAlignmentCenter;
    _elementLabel.textColor = SM_darkGrayColor;
    _elementLabel.highlightedTextColor = SM_thumeColor;
    [self.contentView addSubview:_elementLabel];
    [_elementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(10)));
        make.right.equalTo(@(SM_SIZE(-10)));
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
}

- (void)setOm_selected:(BOOL)om_selected
{
    _om_selected = om_selected;
    _elementLabel.textColor =!om_selected? SM_darkGrayColor:SM_thumeColor;
}


@end



@interface OMCategoryFilterSelectionView ()
<UICollectionViewDelegate,
UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectView;
@end

@implementation OMCategoryFilterSelectionView

- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}


- (void)configure
{
    UIView *backgroundView = [UIView new];
    backgroundView.userInteractionEnabled = true;
    backgroundView.backgroundColor = [SM_blackColor colorWithAlphaComponent:.6];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:nil action:nil];
    [backgroundView addGestureRecognizer:tap];
    @weakify(self);
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        @strongify(self);
        [self dismiss];
    }];
    [self addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];

    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[self collectLayout]];
    [_collectView registerClass:[OMCategoryFilterSelectionCollectCell class] forCellWithReuseIdentifier:[OMCategoryFilterSelectionCollectCell cellIdentifier]];
    _collectView.backgroundColor = SM_whiteColor;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [self addSubview:_collectView];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@0);
    }];
}

CGFloat itemHeight = 0;
- (UICollectionViewFlowLayout *)collectLayout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    itemHeight = SM_SIZE(120);
    float itemSize = (SM_SCREEN_WIDTH*6)/8.f/3;
    layout.itemSize = CGSizeMake(itemSize, itemHeight);
    return layout;
}

- (void)setItemTitles:(NSArray *)itemTitles
{
    _itemTitles = itemTitles;
    [self.collectView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemHeight*(self.itemTitles.count/3+(self.itemTitles.count%3!=0?1:0))));
    }];
}

//MARK: Actions
- (void)dismiss
{
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didDismissFilterSelectView:)]) {
        [self.delegate didDismissFilterSelectView:self];
    }
}

- (void)showInView:(UIView *)view withFrame:(CGRect)rect
{
    [view addSubview:self];
    self.frame = rect;
}

//MARK: collectionView delegate && datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemTitles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMCategoryFilterSelectionCollectCell *cell = [OMCategoryFilterSelectionCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
    NSString *title = self.itemTitles[indexPath.item];
    cell.elementLabel.text = title;
    cell.om_selected = !self.selectedTitle.length?indexPath.item == 0:[self.selectedTitle isEqualToString:title];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(filterSelectView:didSelectItemAtIndex:)]) {
        [self.delegate filterSelectView:self  didSelectItemAtIndex:indexPath.item];
    }
    [self dismiss];
}



@end
